#pragma once


#include<vector>
#include<string>
#include<iostream>

using namespace std;

static const int __stl_num_primes = 28;
static const unsigned long __stl_prime_list[__stl_num_primes] =
{
  53,         97,         193,       389,       769,
  1543,       3079,       6151,      12289,     24593,
  49157,      98317,      196613,    393241,    786433,
  1572869,    3145739,    6291469,   12582917,  25165843,
  50331653,   100663319,  201326611, 402653189, 805306457,
  1610612741, 3221225473, 4294967291
};

inline unsigned long __stl_next_prime(unsigned long n)
{
	const unsigned long* first = __stl_prime_list;
	const unsigned long* last = __stl_prime_list + __stl_num_primes;
	const unsigned long* pos = lower_bound(first, last, n);
	return pos == last ? *(last - 1) : *pos;
}

template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

template<>
struct HashFunc<string>
{
	size_t operator()(const string& s)
	{
		size_t sum = 0;
		for (auto& e : s)
		{
			sum *= 131;
			sum += e;
		}
		return sum;
	}
};

namespace yyh
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;

		HashNode(const T& data = T())
			:_data(data)
			, _next(nullptr)
		{ }
	};

	template<class K, class T, class KeyOft, class Hash>
	class HashTable;

	template<class K, class T, class Ref, class Ptr, class KeyOft, class Hash>
	struct HashTableIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, KeyOft, Hash> HashTable;
		typedef HashTableIterator<K, T, Ref, Ptr, KeyOft, Hash> Self;

		Node* _node;
		const HashTable* _ht;

		HashTableIterator(Node* node, const HashTable* ht)
			:_node(node)
			,_ht(ht)
		{ }

		T& operator*()
		{
			return _node->_data;
		}

		T* operator->()
		{
			return &_node->_data;
		}

		Self& operator++()
		{
			if (_node->_next)
			{
				_node = _node->_next;
			}
			else
			{
				KeyOft kot;
				Hash hs;
				size_t hash_i = hs(kot(_node->_data)) % _ht->_tables.size();
				hash_i++;
				while (hash_i < _ht->_tables.size())
				{
					if (_ht->_tables[hash_i])
					{
						_node = _ht->_tables[hash_i];
						break;
					}
					hash_i++;
				}
				if (hash_i == _ht->_tables.size())
				{
					_node = nullptr;
				}
			}
			return *this;
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}

		bool operator==(const Self& s)
		{
			return _node == s._node;
		}
	};

	template<class K, class T, class KeyOft, class Hash>
	class HashTable
	{
		typedef HashNode<T> Node;
	public:
		typedef HashTableIterator<K, T, T&, T*, KeyOft, Hash> Iterator;
		typedef HashTableIterator<K, T, const T&, const T*, KeyOft, Hash> ConstIterator;

		template<class K, class T, class Ref, class Ptr, class KeyOft, class Hash>
		friend struct HashTableIterator;

		Iterator Begin()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return Iterator(_tables[i], this);
				}
			}
			return Iterator(nullptr, this);
		}

		Iterator End()
		{
			return Iterator(nullptr, this);
		}

		ConstIterator Begin() const
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return ConstIterator(_tables[i], this);
				}
			}
			return End();
		}

		ConstIterator End() const
		{
			return ConstIterator(nullptr, this);
		}

		HashTable(size_t size = __stl_next_prime(0))
			:_tables(size, nullptr)
			, _n(0)
		{ }

		~HashTable()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
		}

		pair<Iterator, bool> Insert(const T& data)
		{
			KeyOft kot;
			Iterator ret = Find(kot(data));
			if (ret != End())
			{
				return { ret, false };
			}
			Hash hs;
			if (_n == _tables.size())
			{
				vector<Node*> new_tables(__stl_next_prime(_tables.size() + 1), nullptr);
				for (int i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hash_i = hs(kot(cur->_data)) % new_tables.size();
						cur->_next = new_tables[hash_i];
						new_tables[hash_i] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(new_tables);
			}
			size_t hash_i = hs(kot(data)) % _tables.size();
			Node* new_node = new Node(data);
			new_node->_next = _tables[hash_i];
			_tables[hash_i] = new_node;
			_n++;
			return { Iterator(new_node, nullptr), true };
		}

		Iterator Find(const K& key)
		{
			Hash hs;
			KeyOft kot;
			size_t hash_i = hs(key) % _tables.size();
			Node* cur = _tables[hash_i];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					return Iterator(cur, nullptr);
				}
				cur = cur->_next;
			}
			return End();
		}

		bool Erase(const K& key)
		{
			Hash hs;
			KeyOft kot;
			size_t hash_i = hs(key) % _tables.size();
			Node* cur = _tables[hash_i];
			Node* prev = nullptr;
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					if (prev == nullptr)
					{
						_tables[hash_i] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					_n--;
					delete cur;
					return true;
				}
				prev = cur;
				cur = cur->_next;
			}
			return false;
		}

	private:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}